Python abs() Function
The Python abs() method returns the absolute value of a number. It accept one argument of type integer or a floating point number. If the input argument is a complex number, its returns the magnitude.
Example 1.
Below is the sample Python program to calculate the absolute value of a integer and floating point number. Write the below script in a python script:
1 2 3 4 5 6 7 8 | # A sample python program to get absolute value of # an integer or floating point number num1 = -10 print abs(num1) num2 = -3.638 print abs(num2) |
Run the script and see the output:
10 3.638
Example 2.
Here is another example of calculating absolute value of a complex number. In result, you will get the magnitude of the number.
Create another Python script and write below value.
1 2 3 4 5 | # A sample python program to get absolute value of # a complex number num1 = (12 - 5j) print abs(num1) |
Run the script and see the output:
13.0